package gonz.grabber.modernbag;

import gonz.common.config.GrabLink;
import gonz.grabber.Grabber;
import gonz.grabber.Item;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

public class ModernbagGrabber extends Grabber {
    public static final String SORT_ID = "cat_product_sort";
    private static final Logger LOGGER = Logger.getLogger(ModernbagGrabber.class);

    @Override
    protected List<Item> grabItems(WebDriver webDriver, GrabLink grabLink) {
        List<Item> items = new ArrayList<Item>();

        String currentItemsPageLink = grabLink.getLink();
        boolean isNextPageExist = true;
        int currentItemsPageNum = 1;
        int currentItemNum = 1;
        do {
            webDriver.get(currentItemsPageLink);
            WebElement sortElement;
            try {
                sortElement = webDriver.findElement(By.id(SORT_ID));
            } catch (NoSuchElementException e) {
                LOGGER.error("Can't find items on page " + currentItemsPageNum + " " + currentItemsPageLink);
                break;
            }
            List<WebElement> itemElements = sortElement.findElements(By.xpath("../table/tbody/tr/td/form/div[@class=\'prdbrief_name\']/a"));
            LOGGER.info("Found " + itemElements.size() + " items on page " + currentItemsPageNum + " " + currentItemsPageLink);
            for (WebElement itemElement : itemElements) {
                String itemLink = itemElement.getAttribute("href");
                String itemName = itemElement.getText();
                items.add(new Item(String.valueOf(currentItemNum), itemName, itemLink));
                currentItemNum++;
            }

            currentItemsPageLink = getNextPageLink(sortElement);
            if (StringUtils.isEmpty(currentItemsPageLink)) {
                isNextPageExist = false;
            } else {
                currentItemsPageNum++;
            }
        } while (isNextPageExist);

        LOGGER.info("Total found " + items.size() + " items. On " + currentItemsPageNum + " pages.");
        LOGGER.info("Starting load images...");
        for (Item item : items) {
            webDriver.get(item.getLink());
            List<String> imgLinks = new ArrayList<String>();

            WebElement mainImgElement = webDriver.findElement(By.id("img-current_picture"));
            WebElement imgParentElement = mainImgElement.findElement(By.xpath(".."));
            String mainImgLink = imgParentElement.getAttribute("href");
            imgLinks.add(mainImgLink);
            try {
                WebElement otherImgsElement = webDriver.findElement(By.id("box_product_thumbnails"));
                List<WebElement> otherImgLinksElement = otherImgsElement.findElements(By.xpath("tbody/tr/td/a"));
                for (WebElement otherImgLinkElement : otherImgLinksElement) {
                    String otherImgLink = otherImgLinkElement.getAttribute("href");
                    imgLinks.add(otherImgLink);
                }
            } catch (NoSuchElementException e) {
                LOGGER.debug("Cant find other images for " + item);
            }
            LOGGER.debug("Found " + imgLinks.size() + " images for " + item);
            item.setImgLinks(imgLinks);
        }
        return items;
    }

    private String getNextPageLink(WebElement sortElement) {
        try {
            WebElement nextPageElement = sortElement.findElement(By.xpath("../p[2]/a[text()=\'след >>\']"));
            return nextPageElement.getAttribute("href");

        } catch (NoSuchElementException e) {
            return null;
        }
    }
}
